{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 函数多返回值\n",
   "id": "3c7d2d1996e54238"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-02-24T08:55:27.380898Z",
     "start_time": "2025-02-24T08:55:27.366401Z"
    }
   },
   "source": [
    "def text_return():\n",
    "    return 1,2\n",
    "\n",
    "x,y=text_return()\n",
    "print(x,y)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 函数多种传参方式\n",
   "id": "9dcdb2f89e976f7c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-24T09:13:02.624550Z",
     "start_time": "2025-02-24T09:13:02.602596Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def user_info(name,age,gender):\n",
    "    print(f\"姓名{name},年龄{age},性别{gender}\")"
   ],
   "id": "8b2e0f91b4cc6930",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 位置参数",
   "id": "d430891f5f0a453e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-24T09:13:06.359646Z",
     "start_time": "2025-02-24T09:13:06.351936Z"
    }
   },
   "cell_type": "code",
   "source": "user_info('小明',20,'男')",
   "id": "a71e9c10e9ca32a5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名小明,年龄20,性别男\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 关键字参数",
   "id": "a8e68e232923f228"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-24T09:14:29.153300Z",
     "start_time": "2025-02-24T09:14:29.140826Z"
    }
   },
   "cell_type": "code",
   "source": "user_info('小明',gender='男',age=18)",
   "id": "55229debd0fd9238",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名小明,年龄18,性别男\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 缺省参数",
   "id": "cef118191cb1affd"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-24T09:16:22.094210Z",
     "start_time": "2025-02-24T09:16:22.080677Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def user_info(name,age,gender='男'):\n",
    "    print(f\"姓名{name},年龄{age},性别{gender}\")\n",
    "user_info(\"小王\",18)"
   ],
   "id": "ee00194e5762e341",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名小王,年龄18,性别男\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 不定长参数",
   "id": "4eb19a04396a067a"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-24T09:35:35.364852Z",
     "start_time": "2025-02-24T09:35:35.354740Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# * 位置\n",
    "def user_info(*args):\n",
    "    print(f\"type{type(args)},{args}\")\n",
    "\n",
    "user_info(1,2,3,'小明','男孩')"
   ],
   "id": "59f0e4826ae95e94",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "type<class 'tuple'>,(1, 2, 3, '小明', '男孩')\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-24T09:38:23.661550Z",
     "start_time": "2025-02-24T09:38:23.650498Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# ** 关键字\n",
    "def user_info(**kwargs):\n",
    "    print(f\"type{type(kwargs)},{kwargs}\")\n",
    "user_info(name='小王',age=11,gender='男孩',addr='北京')"
   ],
   "id": "e8886935b49d4f76",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "type<class 'dict'>,{'name': '小王', 'age': 11, 'gender': '男孩', 'addr': '北京'}\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 匿名函数",
   "id": "1a70b214cef461f5"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 函数作为参数传递",
   "id": "3a725be22b67cc80"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-24T09:49:16.056527Z",
     "start_time": "2025-02-24T09:49:16.039472Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def test_func(compute):\n",
    "    result=compute(1,2)\n",
    "    print(result)\n",
    "\n",
    "def compute(x,y):\n",
    "    return x+y\n",
    "\n",
    "test_func(compute)"
   ],
   "id": "e5664b5597ab3e16",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## lambda匿名函数",
   "id": "8dbadf68d0848521"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-24T09:58:48.168640Z",
     "start_time": "2025-02-24T09:58:48.148891Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def test_func(compute):\n",
    "    result=compute(1,2)\n",
    "    print(result)\n",
    "test_func(lambda x,y:x+y)"
   ],
   "id": "ad92353fe3b093c2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "execution_count": 9
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
